﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Barbar.Colonization.Core.Localization;
using System.Runtime.Serialization;
using Barbar.Colonization.Core;

namespace Barbar.Colonization.Model.RuleSets {
  [XmlType(Namespace = Constants.XmlRootNamespace, TypeName = "TerrainType")]
  public class XmlTerrainType : XmlEntity, ITerrainType, IRulesetDeserialization {
    private TranslatorString m_Name;
    private string m_Code;

    public string Code {
      get { return m_Code; }
      set { m_Code = value; }
    }

    public override string ToString() {
      if (m_Name != null)
        return m_Name.ToString();
      return m_Code;
    }

    #region ITerrainType Members

    TranslatorString ITerrainType.Name {
      get { return m_Name; }
    }

    #endregion

    #region IRulesetDeserialization Members

    void IRulesetDeserialization.OnDeserialization(XmlRuleSet ruleSet) {
      if (String.IsNullOrEmpty(m_Code))
        throw new ColonizationException("Code should be set.");
      m_Name = new TranslatorString(m_Code);
    }

    #endregion
  }
}
